using System;
using System.Collections.Generic;
using System.Text;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using System.Windows.Forms;
using System.IO;
using Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_AUX
{
    /// <summary>
    /// Class containing special tab page ui messaging and control associated properties
    /// </summary>
    public class GenTabPageProperties : IAEUCSerialization
    {
        string tab_name;
        uint tab_id;
        uint tab_id_extra;
        uint tab_rel_msg_id;
        uint tab_rel_rqst_msg_id;
        uint tab_rel_msg_status;
        uint tab_rel_component_id;
        List<IAEUCSetting> tab_cont_settings;
        TabPage tab_page_reference;
        AEUCTabbedParentControl tab_parent_ref;

        /// <summary>
        /// Get set method for the tab page property display name
        /// </summary>
        public string TabName
        {
            get
            {
                return tab_name;
            }
            set
            {
                tab_name = value;
                if (tab_page_reference != null)
                {
                    tab_page_reference.Text = tab_name;
                }
            }
        }
        /// <summary>
        /// Get set method for the tab page property tab id
        /// </summary>
        public uint TabId
        {
            get
            {
                return tab_id;
            }
            set
            {
                tab_id = value;
            }
        }
        /// <summary>
        /// Get set method for the tab page property byte size
        /// </summary>
        public uint TabIdExtra
        {
            get
            {
                return tab_id_extra;
            }
            set
            {
                tab_id_extra = value;
            }
        }
        /// <summary>
        /// Get set method for the tab page property byte size
        /// </summary>
        public uint TabRelMSGId
        {
            get
            {
                return tab_rel_msg_id;
            }
            set
            {
                tab_rel_msg_id = value;
            }
        }
        /// <summary>
        /// Get set method for the tab page property byte size
        /// </summary>
        public uint TabRelRqstMSGId
        {
            get
            {
                return tab_rel_rqst_msg_id;
            }
            set
            {
                tab_rel_rqst_msg_id = value;
            }
        }
        /// <summary>
        /// Get set method for the tab page property byte size
        /// </summary>
        public uint TabRelMSGStatus
        {
            get
            {
                return tab_rel_msg_status;
            }
            set
            {
                tab_rel_msg_status = value;
            }
        }
        /// <summary>
        /// Get set method for the tab page property byte size
        /// </summary>
        public uint TabRelCompId
        {
            get
            {
                return tab_rel_component_id;
            }
            set
            {
                tab_rel_component_id = value;
            }
        }
        /// <summary>
        /// Returns an array of all of the settings available on the tab page
        /// </summary>
        public IAEUCSetting[] GetSettingArray
        {
            get
            {
                return tab_cont_settings.ToArray();
            }
        }

        /// <summary>
        /// Creates the tab page descriptor object.
        /// </summary>
        /// <param name="itab_name">Name of the tab page</param>
        /// <param name="itab_ref">Reference to the TabPage object</param>
        /// <param name="itab_parent_ref">Reference to the tab page parent control</param>
        public GenTabPageProperties(string itab_name, TabPage itab_ref, AEUCTabbedParentControl itab_parent_ref)
        {
            tab_cont_settings = new List<IAEUCSetting>();
            tab_id = 0;
            tab_rel_msg_id = 0;
            tab_rel_msg_status = 0;
            tab_rel_component_id = 0;
            tab_page_reference = itab_ref;
            tab_parent_ref = itab_parent_ref;
            TabName = itab_name;
            itab_parent_ref.OnAEUCControlsUpdated += new AEUCTabbedParentControl.AEUCControlsUpdated(itab_parent_ref_OnAEUCControlsUpdated);
        }

        /// <summary>
        /// Creates the tab page descriptor object.
        /// </summary>
        /// <param name="itab_parent_ref">Reference to the tab page parent control</param>
        public GenTabPageProperties(AEUCTabbedParentControl itab_parent_ref)
        {
            tab_cont_settings = new List<IAEUCSetting>();
            tab_id = 0;
            tab_rel_msg_id = 0;
            tab_rel_msg_status = 0;
            tab_rel_component_id = 0;
            tab_page_reference = null;
            tab_parent_ref = itab_parent_ref;
            tab_name = "";
            itab_parent_ref.OnAEUCControlsUpdated += new AEUCTabbedParentControl.AEUCControlsUpdated(itab_parent_ref_OnAEUCControlsUpdated);
        }

        void itab_parent_ref_OnAEUCControlsUpdated()
        {
            for (int i = 0; i < tab_page_reference.Controls.Count; i++)
            {
                Boolean add_setting = true;
                if (!(tab_page_reference.Controls[i] is IAEUCSetting))
                    continue;
                IAEUCSetting tmpSet = (IAEUCSetting)tab_page_reference.Controls[i];

                for (int k = 0; k<tab_cont_settings.Count; k++)
                {
                    if (tmpSet.AEUC_Id == tab_cont_settings[k].AEUC_Id)
                        add_setting = false;
                }

                if (add_setting)
                {
                    tab_cont_settings.Add(tmpSet);
                }
            }

            //tab_cont_settings.Sort();
        }

        #region IAEUCSerialization Members
        /// <summary>
        /// Current element serialization state version
        /// </summary>
        public int serVersion
        {
            get
            {
                return 0x002;
            }
        }
        /// <summary>
        /// Saves this objec instance to a file
        /// </summary>
        /// <param name="srWriter">Binray writer of the associated file</param>
        public void Serialize(BinaryWriter srWriter)
        {
            srWriter.Write(serVersion);
            Serialize_v002(srWriter);
        }
        /// <summary>
        /// Loads this objects instance from a file
        /// </summary>
        /// <param name="srReader">Binray reader of the associated file</param>
        /// <returns>Boolean representing the success of the operation</returns>
        public bool Deserialize(BinaryReader srReader)
        {
            bool desProcState;
            int serialVer = srReader.ReadInt32();
            
            switch(serialVer)
            {
                case 0x000:
                    desProcState = Deserialize_v000(srReader);
                    break;
                case 0x001:
                    desProcState = Deserialize_v001(srReader);
                    break;
                case 0x002:
                    desProcState = Deserialize_v002(srReader);
                    break;
                default:
                    desProcState = false;
                    break;
            }
            
            return desProcState;
        }

        #region Version 0x000 serialization
        /// <summary>
        /// Version 0x000 serialization
        /// </summary>
        /// <param name="srWriter">File writer</param>
        void Serialize_v000(BinaryWriter srWriter)
        {
            srWriter.Write(tab_name);
            srWriter.Write(tab_id);
            srWriter.Write(tab_parent_ref.TabPages.IndexOf(tab_page_reference));
        }
        /// <summary>
        /// Version 0x000 deserialization
        /// </summary>
        /// <param name="srReader">File reader</param>
        /// <returns>True if operation is succesful</returns>
        bool Deserialize_v000(BinaryReader srReader)
        {
            int tab_index = -1;
            TabName = srReader.ReadString();
            TabId = srReader.ReadUInt32();

            tab_index = srReader.ReadInt32();
            if (tab_index < tab_parent_ref.TabPages.Count)
                tab_page_reference = tab_parent_ref.TabPages[tab_index];
            else
                tab_page_reference = null;

            if (tab_page_reference == null)
                return false;
            else
                return true;
        }
        #endregion
        #region Version 0x001 serialization
        /// <summary>
        /// Version 0x001 serialization
        /// </summary>
        /// <param name="srWriter">File writer</param>
        void Serialize_v001(BinaryWriter srWriter)
        {
            srWriter.Write(tab_name);
            srWriter.Write(tab_id);
            srWriter.Write(tab_id_extra);
            srWriter.Write(tab_rel_msg_id);
            srWriter.Write(tab_rel_msg_status);
            srWriter.Write(tab_rel_component_id);
            srWriter.Write(tab_parent_ref.TabPages.IndexOf(tab_page_reference));
        }
        /// <summary>
        /// Version 0x001 deserialization
        /// </summary>
        /// <param name="srReader">File reader</param>
        /// <returns>True if operation is succesful</returns>
        bool Deserialize_v001(BinaryReader srReader)
        {
            int tab_index = -1;
            TabName = srReader.ReadString();
            TabId = srReader.ReadUInt32();
            TabIdExtra = srReader.ReadUInt32();
            TabRelMSGId = srReader.ReadUInt32();
            TabRelMSGStatus = srReader.ReadUInt32();
            TabRelCompId = srReader.ReadUInt32();

            tab_index = srReader.ReadInt32();
            if (tab_index < tab_parent_ref.TabPages.Count)
                tab_page_reference = tab_parent_ref.TabPages[tab_index];
            else
                tab_page_reference = null;

            if (tab_page_reference == null)
                return false;
            else
                return true;
        }
        #endregion
        #region Version 0x002 serialization
        /// <summary>
        /// Version 0x001 serialization
        /// </summary>
        /// <param name="srWriter">File writer</param>
        void Serialize_v002(BinaryWriter srWriter)
        {
            srWriter.Write(tab_name);
            srWriter.Write(tab_id);
            srWriter.Write(tab_id_extra);
            srWriter.Write(tab_rel_msg_id);
            srWriter.Write(tab_rel_rqst_msg_id);
            srWriter.Write(tab_rel_msg_status);
            srWriter.Write(tab_rel_component_id);
            srWriter.Write(tab_parent_ref.TabPages.IndexOf(tab_page_reference));
        }
        /// <summary>
        /// Version 0x001 deserialization
        /// </summary>
        /// <param name="srReader">File reader</param>
        /// <returns>True if operation is succesful</returns>
        bool Deserialize_v002(BinaryReader srReader)
        {
            int tab_index = -1;
            TabName = srReader.ReadString();
            TabId = srReader.ReadUInt32();
            TabIdExtra = srReader.ReadUInt32();
            TabRelMSGId = srReader.ReadUInt32();
            TabRelRqstMSGId = srReader.ReadUInt32();
            TabRelMSGStatus = srReader.ReadUInt32();
            TabRelCompId = srReader.ReadUInt32();

            tab_index = srReader.ReadInt32();
            if (tab_index < tab_parent_ref.TabPagesL.Length)
                tab_page_reference = tab_parent_ref.TabPagesL[tab_index];
            else
                tab_page_reference = null;

            if (tab_page_reference == null)
                return false;
            else
                return true;
        }
        #endregion
        #endregion
    }
}
